<?php 
    require("3lop/model/model.php");
    	$cmd = $_GET['cmd'];
        switch ($cmd) {      
            case 'SHOW.CODE':                    
                $id = $_GET['id'];             
                $capcha = $_GET['capcha'];
                if($id && $capcha == $_SESSION['capcha']):
                    $muathe = get_thecao($id);
                    require ('3lop/view/getthe.php'); 
                endif;
            break;    
            case 'SHOW.BUY':                    
                $id = $_GET['id'];             
                $capcha = $_GET['capcha'];
                if($id && $capcha == $_SESSION['capcha']):
                    $muathe = get_thecao($id);
                    require ('3lop/view/getthe.php'); 
                endif;
            break;    
            case 'SHOW.CATEGORY':                    
                $id = $_GET['xCategory'];
                if($id==1):
                    require ('3lop/view/topup_mobile.php'); 
                elseif($id==2):
                    require ('3lop/view/topup_game.php'); 
                else:
                    $name = get_cat_name($id);
                    $chitiet = get_cat_product($id);
                    require ('3lop/view/category.php'); 
                endif;
            break;
            case 'SHOW.NEWS';  
                    $page = $_GET['xPage'];
                    $limit = $_GET['xRecord'];
                    $total = get_page();
                    $total2 = ceil($total['total'] / $limit);
                    $news = get_news($page, $limit);
                    require ('3lop/view/news.php');
            break;
            case 'SHOW.HELP';
                    $help = get_help();
                    require ('3lop/view/help.php');
            break;
            case 'SHOW.HUONGDAN';
                    $huongdan = get_huongdan();
                    require ('3lop/view/huongdan.php');
            break;
            case 'SHOW.LIENHE';                
                    require ('3lop/view/lienhe.php');
            break;
            case 'SHOW.ORDER';                
                    require ('3lop/view/order.php');
            break;
            case 'CART.SUMMARY'; 
				$idlist = "";
				if(isset($_SESSION['CART'])){
					$cart=$_SESSION["CART"];
					if($cart){
						$total=0;
						foreach(array_keys($cart) as $value){
							if($idlist == ""){
								$idlist = $value;
							}
							else{
								$idlist = $idlist.",".$value;
							}
						}
					} 
				}
				$viewcart = get_cart($idlist);
				$payment = get_payment();
                require ('3lop/view/cart.php');
            break;
            case 'CART.ADD';  
                    $id = $_REQUEST["xCategory"];
                    $cart = $_SESSION["CART"];
                    if(!$cart){$cart = array();}
			if($cart){
					if(array_key_exists($id,$cart))
					{
						$cart[$id] +=1;
					}else{
						$cart[$id]=1;
					}
				}else{
					$cart[$id]=1;
				}
                    $_SESSION["CART"] = $cart;
                    echo '{"status":true}';
            break;                
            case 'CART.REMOVE';  
                    $id = $_REQUEST["xCategory"];
                    $cart = $_SESSION["CART"];
                    if(!$cart){
					echo '{"status":false,"message":"Xin l\u00f4\u0303i !"}';
                                        break;                                        
                              }
			if($cart){
					if(array_key_exists($id,$cart))
					{
						unset($cart[$id]);
					}
				}else{
					echo '{"status":false,"message":"Xin l\u00f4\u0303i !"}';
                                        break;
				}
                    $_SESSION["CART"] = $cart;
                   echo '{"status":true}';
            break;        
            case 'CART.EMPTY';  
                    $cart = $_SESSION["CART"];
                    if(!$cart){
					echo '{"status":false,"message":"Xin l\u00f4\u0303i !"}';
                                        break;                                        
                              }
					if($cart){
								unset($cart);
						}else{
							echo '{"status":false,"message":"Xin l\u00f4\u0303i !"}';
												break;
						}
					$_SESSION["CART"] = '';
					echo '{"status":true}';
            break;
            case 'CART.DETAILS';  
				$idlist = "";
				if(isset($_SESSION['CART'])){
						$cart=$_SESSION["CART"];
					if($cart){
						$total=0;
						foreach(array_keys($cart) as $value){
							if($idlist == ""){
								$idlist = $value;
							}
							else{
								$idlist = $idlist.",".$value;
							}
						}
					} 
				}
                                $giohang = get_cart($idlist);
				$payment = get_payment();
                require ('3lop/view/giohang.php');
            break;
            case 'CART.UPDATE';  
                    $id = $_REQUEST["xCategory"];
                    $xQuantity = $_REQUEST["xQuantity"];
                    if($xQuantity<=0){
			echo '{"status":false,"message":"Xin l\u00f4\u0303i, Số Lượng phải lớn hơn 1 !"}';
			break;
                    }
                    $cart = $_SESSION["CART"];
                    if(!$cart){$cart = array();}
			if($cart)
				{
					$cart[$id] = $xQuantity;
				}
                    $_SESSION["CART"] = $cart;
                    echo '{"status":true}';
            break;
            case 'COUPON';  					
                    echo '{"status":false,"message":"Không tồn tại mã giảm giá này"}';
            break;
                
                
                
            default:
            break;
}
?>